package com.ecmap.hongyun.commons.utils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.Random;

import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.web.context.WebApplicationContext;

import com.ecpark.commons.utils.InitSetting;

/**
 * 
 * @className:CommonUtil.java
 * @classDescription:公共工具
 * @author:DANIEL DENG
 * @createTime:2011-7-22
 */
public class CommonUtil {
	
	/**
	 * 获取订单号
	 * @return
	 */
	public static String getOrderNumber(){
		return "#"+DateFormatUtils.format(new Date(), "yyyyMMdd")+getFixLenthString(6);
	}
	
	/**
	 * 消费验证码
	 * @return
	 */
	public static String getVCode(){
		return getFixLenthString(4);
	}
	
	/**
	 * 电子序列号
	 * @return
	 */
	public static String getSerialNo(){
		return "001"+getFixLenthString(7);
	}
	
	/**
	 * 获取商户账号
	 * @return
	 */
	public static String getProviderAccount(){
		return getFixLenthString(4);
	}
	
	/**
	 * 获取采购方账号
	 * @return
	 */
	public static String getPurchaserAccount(){
		return getFixLenthString(4);
	}
		
	/**
	 * 获取商户账号名
	 * @return
	 */
	public static String getOperatorAccountForProvider(){
		return "30"+getFixLenthString(4);
	}
	
	/**
	 * 获取固定长度的随机数字字符串，不足位数在前面补0
	 * @param strLength 字符串长度
	 * @return
	 */
	public  static String getFixLenthString(int strLength) {  
	      
	    Random rm = new Random();  
	    // 返回固定的长度的随机数  
	    String rmResult = String.valueOf( rm.nextInt((int)Math.pow(10,strLength*2)) ); 
	    int len = rmResult.length() - strLength;
	    if( len > 0 ){
	    	//大于0，表示长度过长，从左到右截取
	    	rmResult = rmResult.substring(rmResult.length() - strLength, rmResult.length());
	    }else if( len < 0 ){
	    	//小于0，表示长度不足，在前面补0
	    	String prefix = "" ;
	    	for( int i = 0 ; i > len ; i-- ){
	    		prefix += "0";
	    	}
	    	rmResult = prefix+ rmResult;
	    }
	    
	    return rmResult;   
	} 
	
	/**
	 * 根据键key从系统配置信息中获取相应的值
	 * 
	 * @author DANIEL DENG
	 * @createTime 2011-7-22
	 * @lastModify DANIEL DENG 2011-7-22
	 * @param key 键
	 * @return 值
	 */
	public static String getValueByKey( String key ){
		return InitSetting.getValueByKey(key);
	}

	/**
	 * 验证字符串是否为空.
	 * 
	 * @author DANIEL DENG
	 * @createTime 2011-7-22
	 * @lastModify DANIEL DENG 2011-7-22
	 * @param testString 目标字符串
	 * @return boolean值 ，true为空，false不为空
	 */
	public static boolean isEmpty(String testString) {
		return testString == null || testString.length() == 0;
	}
	
	
	/**
	 * 获取spring上下文，用于动态灵活获取spring的bean
	 * 
	 * @author DANIEL DENG
	 * @createTime 2011-7-22
	 * @lastModify DANIEL DENG 2011-7-22
	 * @return Web应用上下文
	 */
	public static WebApplicationContext getApplicationContext() {
		return InitSetting.getApplicationContext();
	}

	 /**
	  * SHA加密
	  * 
	  * @author DANIEL DENG
	  * @createTime 2011-9-9
	  * @lastModify DANIEL DENG 2011-9-9
	  * @param targetString 需加密的目标字符串
	  * @return sha加密后的字符串
	 * @throws NoSuchAlgorithmException 
	  */
	 public static String encryptSHA( String targetString ){
		 
		 try{
	        MessageDigest sha = MessageDigest.getInstance("SHA-1");  
	        sha.update( targetString.getBytes() );  
	        BigInteger result = new BigInteger(sha.digest());
	        return result.toString();
		 }catch(Exception e){
			 
		 }
        return null;
	 }
	 
	 /**
	  * 获取字符串MD5
	  * 
	  * @author DANIEL DENG
	  * @createTime 2012-2-8
	  * @lastModify DANIEL DENG 2012-2-8
	  * @param source
	  * @return
	  */
	public static String getMD5(String source) {

		String s = null;
		// 用来将字节转换成 16 进制表示的字符
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'a', 'b', 'c', 'd', 'e', 'f' };
		try {
			byte[] b = source.getBytes("UTF-8");
			java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
			md.update(b);
			byte tmp[] = md.digest();
			char str[] = new char[16 * 2];
			int k = 0;
			for (int i = 0; i < 16; i++) {

				byte byte0 = tmp[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];

				str[k++] = hexDigits[byte0 & 0xf];
			}
			s = new String(str);

		} catch (Exception e) {
			//e.printStackTrace();
		}
		return s;
	}
	
	public static void main(String[] args) {
			System.out.println(encryptSHA("129993456"));
			System.out.println(getMD5("350014023554160727838757488446172702700203740733"));
	}
		
}
